package com.quwan.manager.controller;

import com.quwan.common.BaseController;
import com.quwan.common.result.ApiResult;
import com.quwan.common.result.PageResult;
import com.quwan.entity.TAdmin;
import com.quwan.entity.TMessage;
import com.quwan.entity.TMsgRecord;
import com.quwan.entity.TSystemPushMsg;
import com.quwan.manager.service.SystemMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author quan
 * @date 2021-11-22 1:28
 */
@Api(tags = "后台消息推送")
@RestController
@RequiredArgsConstructor
@RequestMapping("mgr/msg")
public class SystemMessageController extends BaseController {
    private final SystemMessageService messageService;
    @ApiOperation("离线消息列表")
    @PostMapping("offlineMsgList")
    public ApiResult<PageResult<TMessage>> offlineMsgList(@RequestParam(defaultValue = "1") int pageNo,
                                                          @RequestParam(defaultValue = "10") int pageSize) {
        return ApiResult.success(messageService.offlineMsgList(pageNo, pageSize));
    }

    @ApiOperation("推送消息列表")
    @PostMapping("msgList")
    public ApiResult<PageResult<TSystemPushMsg>> createMsg(@RequestParam(defaultValue = "1") int pageNo,
                                                           @RequestParam(defaultValue = "10") int pageSize,
                                                           @RequestParam(defaultValue = "1", required = false) String type,
                                                           @RequestParam(required = false) String mId) {
        return ApiResult.success(messageService.msgList(pageNo, pageSize, type, mId));
    }

    @ApiOperation("创建推送消息")
    @PostMapping("create")
    public ApiResult createMsg(HttpServletRequest request,
                               @ApiParam("消息内容") @RequestParam String body,
                               @ApiParam("1:系统消息，其他") @RequestParam String type) {

        TokenInfo tokenInfo = getTokenInfo(request);
        TAdmin admin = new TAdmin().setLoginName(tokenInfo.getUsername()).setId(Long.parseLong(tokenInfo.getId()));
        messageService.createMsg(admin, body, type);
        return ApiResult.success();

    }

    @ApiOperation("推送个人消息")
    @PostMapping("push/personal")
    public ApiResult createPersonalMsg(HttpServletRequest request,
                                       @ApiParam("目标用户") @RequestParam String mId,
                                       @ApiParam("消息内容") @RequestParam String body) {

        TokenInfo tokenInfo = getTokenInfo(request);
        TAdmin admin = new TAdmin().setLoginName(tokenInfo.getUsername()).setId(Long.parseLong(tokenInfo.getId()));
        messageService.createPersonalMsg(admin, mId, body);
        return ApiResult.success();

    }

    @ApiOperation("实名认证消息推送")
    @PostMapping("push/personal/where")
    public ApiResult createPersonalMsg(HttpServletRequest request ,
                                       @ApiParam("实名") @RequestParam String idStatus,
                                       @ApiParam("目标用户") @RequestParam String sex,
                                       @ApiParam("消息内容") @RequestParam String body) {

        TokenInfo tokenInfo = getTokenInfo(request);
        TAdmin admin = new TAdmin().setLoginName(tokenInfo.getUsername()).setId(Long.parseLong(tokenInfo.getId()));
        messageService.pushRealNamePersonalMsg(admin,idStatus,sex,body);
        return ApiResult.success();

    }
    @ApiOperation("离线消息推动送")
    @PostMapping("push/offline/all")
    public ApiResult pushOfflineAll(HttpServletRequest request,String body,String title) {

        TokenInfo tokenInfo = getTokenInfo(request);
        TAdmin admin = new TAdmin().setLoginName(tokenInfo.getUsername()).setId(Long.parseLong(tokenInfo.getId()));
        messageService.pushOfflineMsg(admin,body,title);
        return ApiResult.success();

    }

    @ApiOperation("删除——消息")
    @PostMapping("delMessage")
    public ApiResult delMessage(@ApiParam("主键id") @RequestParam Long id) {
        messageService.delSystemMsg(id);
        return ApiResult.success();

    }

    @ApiOperation("聊天记录")
    @PostMapping("chatRecord")
    public ApiResult<PageResult<TMsgRecord>> chatRecord(@RequestParam(defaultValue = "1") int pageNo,
                                                        @RequestParam(defaultValue = "20") int pageSize,
                                                        @ApiParam("发送人") String sender,
                                                        @ApiParam("接收人") String receiver,
                                                        @ApiParam("会话id") String sessionKey,
                                                        @ApiParam("内容") String content,
                                                        @RequestParam Integer status) {
        PageResult<TMsgRecord> ret = messageService.getChatRecordList(pageNo, pageSize, sender, receiver, sessionKey, content, status);

        return ApiResult.success(ret);

    }

    @ApiOperation("聊天消息修改状态")
    @PostMapping("chatRecord/status")
    public ApiResult changeChatRecordStatus(@RequestParam List<Long> ids,
                                                                    @RequestParam Integer status) {
        messageService.changeChatRecordStatus(ids, status);
        return ApiResult.success();

    }
}
